package cn.tedu.egweibo5.base.interceptors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: Miyako
 * @CreateTime: 2024-08-14  20:18
 * @Description: TODO
 */
@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("Check login status.....");
        Object user = request.getSession().getAttribute("user");
        if (user == null) {
            log.debug("未登录");
            response.sendRedirect("/login.html");
            return false;
        }
        log.debug("已登录");
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.debug("2.LoginCheckInterceptor--postHandle");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.debug("3.LoginCheckInterceptor--afterCompletion");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
